const { sequelize } = require('../dbConnect');

// 导入所有模型
const User = require('./userModel');
const { Course, CourseCategory, UserCourseProgress } = require('./courseModel');
const { AchievementDefinition, UserAchievement } = require('./achievementModel');
const { Activity, Banner } = require('./activityModel');
const { DailySentence, DailyRecord } = require('./dailyModel');
const { DetectionText, EvaluationRecord } = require('./detectionModel');
const { GameContent, GameRecord } = require('./gameModel');
const { FunctionModule, Notification, Recommendation } = require('./systemModel');
const { FileUpload, UserStudyStatistics } = require('./fileModel');

// 定义用户相关关联关系
User.hasMany(EvaluationRecord, {
  foreignKey: 'user_id',
  as: 'evaluationRecords'
});

EvaluationRecord.belongsTo(User, {
  foreignKey: 'user_id',
  as: 'user'
});

EvaluationRecord.belongsTo(DetectionText, {
  foreignKey: 'text_id',
  as: 'detectionText'
});

DetectionText.hasMany(EvaluationRecord, {
  foreignKey: 'text_id',
  as: 'evaluationRecords'
});

User.hasMany(GameRecord, {
  foreignKey: 'user_id',
  as: 'gameRecords'
});

GameRecord.belongsTo(User, {
  foreignKey: 'user_id',
  as: 'user'
});

GameRecord.belongsTo(GameContent, {
  foreignKey: 'content_id',
  as: 'gameContent'
});

GameContent.hasMany(GameRecord, {
  foreignKey: 'content_id',
  as: 'gameRecords'
});

User.hasMany(DailyRecord, {
  foreignKey: 'user_id',
  as: 'dailyRecords'
});

DailyRecord.belongsTo(User, {
  foreignKey: 'user_id',
  as: 'user'
});

DailyRecord.belongsTo(DailySentence, {
  foreignKey: 'sentence_id',
  as: 'dailySentence'
});

DailySentence.hasMany(DailyRecord, {
  foreignKey: 'sentence_id',
  as: 'dailyRecords'
});

// 课程相关关联关系已在courseModel.js中定义

User.hasMany(UserCourseProgress, {
  foreignKey: 'user_id',
  as: 'courseProgress'
});

UserCourseProgress.belongsTo(User, {
  foreignKey: 'user_id',
  as: 'user'
});

UserCourseProgress.belongsTo(Course, {
  foreignKey: 'course_id',
  as: 'course'
});

Course.hasMany(UserCourseProgress, {
  foreignKey: 'course_id',
  as: 'userProgress'
});

// 成就相关关联关系
User.hasMany(UserAchievement, {
  foreignKey: 'user_id',
  as: 'userAchievements'
});

UserAchievement.belongsTo(User, {
  foreignKey: 'user_id',
  as: 'user'
});

// 通知相关关联关系
User.hasMany(Notification, {
  foreignKey: 'user_id',
  as: 'notifications'
});

Notification.belongsTo(User, {
  foreignKey: 'user_id',
  as: 'user'
});

// 文件上传相关关联关系
User.hasMany(FileUpload, {
  foreignKey: 'user_id',
  as: 'fileUploads'
});

FileUpload.belongsTo(User, {
  foreignKey: 'user_id',
  as: 'user'
});

// 用户统计关联关系
User.hasOne(UserStudyStatistics, {
  foreignKey: 'user_id',
  as: 'studyStatistics'
});

UserStudyStatistics.belongsTo(User, {
  foreignKey: 'user_id',
  as: 'user'
});

// 推荐内容与分类关联关系
Recommendation.belongsTo(CourseCategory, {
  foreignKey: 'category_id',
  as: 'courseCategory'
});

CourseCategory.hasMany(Recommendation, {
  foreignKey: 'category_id',
  as: 'recommendations'
});

// 导出所有模型
module.exports = {
  sequelize,
  User,
  Course,
  CourseCategory,
  UserCourseProgress,
  AchievementDefinition,
  UserAchievement,
  Activity,
  Banner,
  DailySentence,
  DailyRecord,
  DetectionText,
  EvaluationRecord,
  GameContent,
  GameRecord,
  FunctionModule,
  Notification,
  Recommendation,
  FileUpload,
  UserStudyStatistics
};
